# -*- coding: utf-8 -*-

from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed, FileRequired
from flask_uploads import UploadSet, IMAGES
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, EqualTo, Email

# todo: 使用falsk_uploads
# images = UploadSet('images', IMAGES)

ALLOWED_UPLOAD_IMG_EXT = ['jpg', 'png']


class NullableEmail(Email):
    def __call__(self, form, field):
        data = field.data or ''
        if data != '':
            super(NullableEmail, self).__call__(form, field)


class GzoneBaseForm(FlaskForm):
    class Meta:
        # TODO: enable csrf
        csrf = False


class AnnouncementForm(GzoneBaseForm):
    title = StringField(u'标题', validators=[DataRequired(u'请输入标题')])
    content = StringField(u'内容', validators=[DataRequired(u'请输入群内容')])
    image = FileField(
        u'附图',
        validators=[
            FileAllowed(ALLOWED_UPLOAD_IMG_EXT, u'不支持图片之外类型')
        ]
    )
    submit = SubmitField(u'添加')


class MomentForm(GzoneBaseForm):
    # content = StringField(u'内容', validators=[DataRequired(u'请输入群友圈状态内容')])
    content = StringField(u'内容')
    images = FileField(u'群友圈图片')


class AccountForm(GzoneBaseForm):
    email = StringField(
        u'邮箱',
        validators=[
            NullableEmail(u'请正确邮箱格式')
        ]
    )
    tel = StringField(u'电话')
    company = StringField(u'公司')
    title = StringField(u'职务')
    desc = StringField(u'描述')
    banner = FileField(u'背景图片')


class AlbumForm(GzoneBaseForm):
    images = FileField(u'群相册照片', validators=[FileRequired(u'请选择图片')])
    submit = SubmitField(u'上传')


class GroupFileForm(GzoneBaseForm):
    files = FileField(u'群文件', validators=[FileRequired(u'请选择文件')])
    submit = SubmitField(u'上传')


class LoginForm(GzoneBaseForm):
    name = StringField(
        u'用户名',
        validators=[DataRequired(u'请输入用户名')]
    )
    password = PasswordField(
        u'密码',
        validators=[DataRequired(u'请输入密码')]
    )
    submit = SubmitField(u'登录')


class SignupForm(GzoneBaseForm):
    name = StringField(
        u'用户名',
        validators=[DataRequired(u'请输入用户名')]
    )
    password = PasswordField(
        u'密码',
        validators=[DataRequired(u'请输入密码')]
    )
    confirm = PasswordField(
        u'确认密码',
        validators=[
            DataRequired(u'请确认密码'),
            EqualTo('password', u'密码不相同，请再次确认大小写正确后输入')
        ]
    )
    submit = SubmitField(u'注册')
